package com.lanhe.admin.controller.product;


import com.lanhe.common.annotation.Log;
import com.lanhe.common.constant.Constants;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.product.domain.LhHaokaProduct;
import com.lanhe.product.service.ILhHaokaProductService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 号卡产品表(LhHaokaProduct)表控制层
 *
 * @author zhanggl
 * @since 2023-12-25 20:19:27
 */
@RestController
@RequestMapping("lhHaokaProduct")
public class LhHaokaProductController extends BaseController {

	/**
	 * 服务对象
	 */
	@Resource
	private ILhHaokaProductService lhHaokaProductService;

	/**
	 * 分页查询所有数据
	 */
	@PreAuthorize("@ss.hasPermi('product:lhHaokaProduct:list')")
	@GetMapping("/list")
	public TableDataInfo list(LhHaokaProduct product) {
		startPage();
		List<LhHaokaProduct> list = lhHaokaProductService.selectHaokaProductList(product);
		for (LhHaokaProduct haokaProduct : list) {
			haokaProduct.setTypeName(Constants.HaokaType.getHaokaTypeName(haokaProduct.getType()));
		}
		return getDataTable(list);
	}

	/**
	 * 根据编号获取详细信息
	 */
	@PreAuthorize("@ss.hasPermi('product:lhHaokaProduct:query')")
	@GetMapping(value = {"/", "/{productId}"})
	public AjaxResult getInfo(@PathVariable(value = "productId") Long productId) {
		return success(lhHaokaProductService.selectHaokaProductById(productId));
	}

	/**
	 * 新增
	 */
	@PreAuthorize("@ss.hasPermi('product:lhHaokaProduct:add')")
	@Transactional
	@Log(title = "号卡产品管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody LhHaokaProduct product) {

		product.setCreateBy(getUsername());
		product.setCreateTime(new Date());
		return toAjax(lhHaokaProductService.insertHaokaProduct(product));
	}

	/**
	 * 修改
	 */
	@PreAuthorize("@ss.hasPermi('product:lhHaokaProduct:edit')")
	@Log(title = "号卡产品管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody LhHaokaProduct product) {
		if (ObjectUtils.isEmpty(product.getProductId()) || null == product.getProductId()) {
			return error("主键ID不能为空");
		}
		product.setUpdateBy(getUsername());
		product.setUpdateTime(new Date());
		return toAjax(lhHaokaProductService.updateHaokaProduct(product));
	}

	/**
	 * 删除
	 */
	@PreAuthorize("@ss.hasPermi('product:lhHaokaProduct:remove')")
	@Log(title = "号卡产品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
	public AjaxResult remove(@PathVariable Long[] productIds) {
		if (ObjectUtils.isEmpty(productIds)) {
			return error("主键ID不能为空");
		}
		return toAjax(lhHaokaProductService.deleteHaokaProductByIds(productIds));
	}
}

